import Dialog from '@vant/weapp/dialog/dialog'
import api from '../../api/api'
import Toast from '@vant/weapp/toast/toast'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tk: wx.getStorageSync('token') ? wx.getStorageSync('token') : '',
    cells: [
      {
        name: '/pages/my-info/my-info',
        title: '个人资料'
      }, {
        name: '/pages/my-orders/my-orders',
        title: '我的订单'
      }, {
        name: '/pages/my-collect/my-collect',
        title: '我的收藏'
      }, {
        name: '/pages/my-address/my-address',
        title: '地址管理'
      }, {
        name: '/pages/center/center',
        title: '安全中心'
      }
    ],
    user: {}
  },
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  async getUserData() {
    const res = await api.getUser({ tokenString: wx.getStorageSync('token') })
    console.log('用户信息', res);
    this.setData({
      user: res.data.result[0]
    })
  },

  // 更新头像
  async updateAvatar(e) {
    let that = this
    const { file } = e.detail
    // 此时可以自行将文件上传至服务器
    // 图片类型
    const imgType = file.thumb.split('.')[1]
    let img = e.detail.file
    if (!Array.isArray(img)) {
      img = [img];
    }
    img.map(function (v, k) {
      let imgs = v.url;
      wx.getFileSystemManager().readFile({
        filePath: imgs,
        encoding: 'base64',
        success(result) {
          api.updateAvatar({ tokenString: wx.getStorageSync('token'), imgType, serverBase64Img: result.data })
            .then(res => {
              console.log('上传头像', res)
              if (res.data && res.data.code == "H001") {
                Toast.success(res.data.msg)
                that.getUserData()
              } else if (res.data) {
                Toast.fail(res.data.msg)
              }
            })
        }
      });
    })
  },

  // 更新背景
  async updateUserBg(e) {
    let that = this
    const { file } = e.detail
    // 此时可以自行将文件上传至服务器
    // 图片类型
    const imgType = file.thumb.split('.')[1]
    let img = e.detail.file
    if (!Array.isArray(img)) {
      img = [img];
    }
    img.map(function (v, k) {
      let imgs = v.url;
      wx.getFileSystemManager().readFile({
        filePath: imgs,
        encoding: 'base64',
        success(result) {
          api.updateUserBg({ tokenString: wx.getStorageSync('token'), imgType, serverBase64Img: result.data })
            .then(res => {
              console.log('上传背景', res)
              if (res.data && res.data.code == "I001") {
                Toast.success(res.data.msg)
                that.getUserData()
              } else if (res.data) {
                Toast.fail(res.data.msg)
              }
            })
        }
      });
    })
  },
  onLoad(options) {
    let token = wx.getStorageSync('token')
    if (!token) {
      Dialog.alert({
        message: '请登录',
      }).then(() => {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      })
      return
    }
    this.getUserData()
  },
  onShow() {
    let token = wx.getStorageSync('token')
    if (!token) {
      Dialog.alert({
        message: '请登录',
      }).then(() => {
        wx.navigateTo({
          url: '/pages/login/login',
        })
      })
      return
    }
    this.getUserData()
  },
})